# _*_coding:utf-8 _*_
# @Time    :2020/12/3 14:58
# @Author  :lyc
# @Email   :lyc0209@qq.com
# @FileName:Session.py

from Tool.DBTool import DBTool
import base64

def set_auth(username):
    bytes_url = username.encode("utf-8")
    str_url = base64.b64encode(bytes_url)
    return str_url

def get_auth(code):
    str_url = base64.b64decode(code).decode("utf-8")
    return str_url

def check_auth(code):
    db = DBTool()
    sql = "select * from user where username=?"
    ob = [(get_auth(code))]
    result = db.executeQuery(sql, ob)
    
    for s in result:
        if s is not None:
            return True
    return False

def read_session(session):
    return session.get('username')


def check_session(session):
    db = DBTool()
    sql = "select * from user where username=?"
    ob = [(read_session(session))]
    result = db.executeQuery(sql, ob)

    for s in result:
        if s is not None:
            return True
    return False
